<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>_${customer.customerName}_入库点收</title>
<style type="text/css">
	.chosen-container {
		width: 100%!important;
	}
</style>
<script type="text/javascript" src="${ctx}/static/v2/js/modal.js"></script>
<script type='text/javascript'
      src='http://wms.ttyuncang.com/WebSpeech.js'></script>
<script type="text/javascript">

  $(function() {
       //如果点收已经完成，隐藏操作按钮
	   var status='${task.status}';
	   if(status!='token' && status!='doing'){
	      $("button").hide();   
	   };
	   
	   $("#txt_productDate").datepicker({
			format : 'yyyy-mm-dd'
		});
	   
	   $("#txt_expireDate").datepicker({
			format : 'yyyy-mm-dd'
		}); 
		$('#lock_num').on('ifChecked', function (event) {
			$("#txt_num").attr("readOnly", true);
        });
        $('#lock_num').on('ifUnchecked', function (event) {
        	$("#txt_num").removeAttr("readOnly");
        	$("#txt_num").val('');
        });
  	});

  	WebSpeech.ready(function() {
	  	WebSpeech.speak('听到声音了吗？');
  	});

  	var batchTime = 0;
	function keydown_key() {
		var lock_num = $("#lock_num").parent().attr('aria-checked');
		keydown_1();
	}

  	function keydown_1(){
  		var lock_num = $("#lock_num").parent().attr('aria-checked');
	  	var customerCode='${task.customerCode}';
	  	var taskCode='${task.taskCode}';
	  	var billCode='${task.billCode}';
	  	var sku = $("#txt_sku_1").val().trim();
	  	$.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/findSku',
			data:{
				taskCode:taskCode,
				billCode:billCode,
				customerCode:customerCode,
				sku:sku
			},
			dataType:'json',
			async: false,
			cache: false,
			success:function(data) {
				if(data.status == "success"){
					batchTime = 1;
					if(data.isShelfLifeMgmt != 0 || data.isBatchMgmt != 0){
						var batchList, batchCode_modal, productDate_modal, expireDate_modal, model_data;
						batch(data.isShelfLifeMgmt, data.isBatchMgmt, data.batchList ,function(batchList){
							batchCode_modal = batchList[0].batch_input == undefined ? '' : batchList[0].batch_input;
							productDate_modal = batchList[1].batch_start == undefined ? '' : batchList[1].batch_start;
							expireDate_modal = batchList[2].batch_end == undefined ? '' : batchList[2].batch_end;
							if (data.isBatchMgmt != 0) {
								$("#txt_batchCode").val(batchCode_modal);
								$('.txt_batchCode').css('display','inline-block');
							}
							if (data.isShelfLifeMgmt != 0) {
								$("#txt_productDate").val(productDate_modal);
								$("#txt_expireDate").val(expireDate_modal);
								$('.txt_expireDate').css('display','inline-block');
							}
							$('#skuNum').focus();
							if(lock_num == 'true'){
					  		  	numAdd();
					  	  	}else{
					  		  	$("#txt_num").focus();
					  	  	}
						});
					}else{
						batchCode = '';
						productDate = '';
						expireDate = '';
						if(lock_num == 'true'){
				  		  	numAdd();
				  	  	}else{
				  		  	$("#txt_num").focus();
				  	  	}
					}
			    }else if(data.status == "skuNotExist"){
			       alert(data.content);
			       $("#txt_sku_1").val('');
			       $("#txt_sku_1").focus();
			       if(!(lock_num == 'true')){
			       		$('#txt_num').val('');
			       }
			    }else {
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   	});
  	}
  
  	function errorChange(){
	  	var lock_num = $("#lock_num").is(":checked");
	  	if(!lock_num){
		  	$("#txt_num").val('');
      	}
	  	$("#txt_sku_1").val('');
	  	$("#txt_batchCode").val('');
	  	$("#txt_productDate").val('');
	  	$("txt_expireDate").val('');
  	}

  	// 数量变化时判断是否验证弹框
	function numChange() {
		if (batchTime == 0) {
			keydown_1();
		}
	}
  
  	var curSku;
  
  	function numAdd(){
	  
  	  var errorflag=$("#error_flag").val();
	  var sku=$("#txt_sku_1").val().trim();
	  var batchCode=$("#txt_batchCode").val().trim();
	  var productDate=$("#txt_productDate").val().trim();
	  var expireDate=$("#txt_expireDate").val().trim();
	  
	  var num=$("#txt_num").val().trim();
	  if(errorflag==0){
	  	  $("#msg_1").css("color","red");
		  $("#msg_1").html("请选择报损的状态");
		  $("#error_flag").select();
		  $("#error_flag").focus();
		  return false;
	  }
	  
	  if(sku.length==0){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("请输入sku!");
		  $("#txt_sku_1").select();
		  $("#txt_sku_1").focus();
		  return false;
	  }
	  
	  if(num.length==0){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("请输入数量!");
		  $("#txt_num").select();
		  $("#txt_num").focus();
		  return false;
	  }
	  
	  if(isNaN(num)){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("请输入正确的数字!");
		  $("#txt_num").select();
		  $("#txt_num").focus();
		  return false;
	  }
	  if(num == 0){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("点收数量不能为0!");
		  $("#txt_num").select();
		  $("#txt_num").focus();
		  return false;
	  }
	  
	  var taskCode='${task.taskCode}';
	  var customerCode='${task.customerCode}';
	  var billCode='${task.billCode}';
	  
      $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/ajaxCountNew',
			data:{
				billCode:billCode,
				customerCode:customerCode,
				taskCode:taskCode,
				sku:sku,
				batchCode:batchCode,
				productDate:productDate,
				expireDate:expireDate,
				num:num,
				errorflag:errorflag
			},
			dataType:'json',
			success:function(data) {
				var status=data.status;
				var content = data.content;
				var goodNum = data.goodNum;
				var newFlag = data.newFlag;
				var rowNum = 1 ;
			    if( status=='success'){
			       
			       var items=data.items;
			       
			       $("#boxItem").html('');
			       var str='';
			       $.each(items,function(i,item){
			    	   if(item.num == item.planNum){
			    		   str+="<tr id='tr_"+item.id+"'>";
			    	   }else{
			    		   str+="<tr id='tr_"+item.id+"' style='background:#FC554F;'>";
			    	   }
			    	   str+="<td>"+rowNum+"</td>";
			    	   rowNum++ ;
			    	   str+="<td>"+item.sku+"</td>";
			    	   str+="<td>"+item.ediGoodsSku.skuName+"</td>";
			    	   str+="<td>"+item.num+"</td>";
			    	   str+="<td>"+item.planNum+"</td>";
			    	   str+="<td>"+item.batchCode+"</td>";
			    	   str+="<td>"+item.productDate+"</td>";
			    	   str+="<td>"+item.expireDate+"</td>";
			    	   if(item.errorFlag=='noterror'){
			    	 	  str+="<td>完好件</td>";
			    	   }else if(item.errorFlag=='error'){
			    	 	  str+="<td>破损件</td>";
			    	   }
			    	   str+="<td><a class='btn btn-warning' onclick='deleteItem(\""+item.id+"\");'>删除</a><td>";
			    	   str+="</tr>";
			       });
			       $("#boxItem").html(str);
			    	
			       $("#txt_sku_1").val('');
			       $("#txt_batchCode").val('');
				   $("#txt_productDate").val('');
				   $("#txt_expireDate").val('');

			       $("#error_flag").val('noterror');
			   	   $("#txt_sku_1").focus();
			   	   
			       $("#msg_1").css("color","green");
				   $("#msg_1").html("点收成功。预约数量:"+ goodNum);
				   if(newFlag == "yes")
				   {
					   WebSpeech.speak('新商品');
				   }
				   else if(curSku!=sku && newFlag == "no")
				   {
					   WebSpeech.speak('已有新商品');
				   }
				   else
				   {
					   WebSpeech.speak('正确');
				   }
				   curSku=sku;
			    }else if(status == 'skuNotExist'){
			    	$("#msg_1").css("color","red");
					$("#msg_1").html(content);
					$("#txt_sku_1").focus();
					$("#txt_sku_1").select();
					WebSpeech.speak('商品不存在');
					$("#txt_sku_1").val('');
					$("#txt_sku_1").focus();
				}else if(status == 'NoGoodSku'){
					$("#msg_1").css("color","red");
					$("#msg_1").html("该SKU没有配置商品信息,请先配置！");
					$("#txt_sku_1").focus();
					$("#txt_sku_1").select();
				}else if(status == 'NoConfigure'){
					$("#msg_1").css("color","red");
					$("#msg_1").html("该SKU没有配置长宽高,请先配置！");
					$("#txt_sku_1").focus();
					$("#txt_sku_1").select();
				}else if(status == 'error'){
			    	$("#msg_1").css("color","red");
					$("#msg_1").html(content);
					$("#txt_sku_1").focus();
					$("#txt_sku_1").select();
					WebSpeech.speak('异常');
					$("#txt_sku_1").val('');
					$("#txt_batchCode").val('');
				    $("#txt_productDate").val('');
				    $("#txt_expireDate").val('');
					$("#txt_sku_1").focus();
			    }else{
			      alert("网络繁忙，请稍后重试...");
			    }
			    $("#txt_sku_1").val('');
			    $("#txt_batchCode").val('');
			    $("#txt_productDate").val('');
			    $("#txt_expireDate").val('');
			  	$('.txt_batchCode').css('display','none');
			  	$('.txt_expireDate').css('display','none');
			  	var lock_num = $("#lock_num").parent().attr('aria-checked');
				if (!(lock_num == 'true')) {
					$("#txt_num").val('');
					$("#txt_sku_1").focus();
				}
				clearBatch();
				batchTime = 0;
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
  	}
  //删除
  function deleteItem(itemId){
	  $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/ajaxDeleteCountItem',
			data:{
				itemId:itemId
			},
			dataType:'json',
			success:function(data) {
			    if( data==true){
				   $("#tr_"+itemId).remove();
			    }else{
			      alert(data);
			    }
			},
			error:function(data) {
				alert(data.responseText);
			}
	   });
  }
  
  //完成点收
  function finish(){
      
     $("button").attr("disabled",true);
     $("button").attr("class","btn");
	 if($("#singleItem").children().length == 0 && $("#boxItem").children().length == 0){
	 	alert("点收数据不能为空");
	 	return;
	 }
  
	  $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/ajaxFinishCount',
			data:{
				taskCode:'${task.taskCode}',
				billCode:'${task.billCode}'
			},
			dataType:'json',
			success:function(data) {
			    if(data.result==true){
			    	window.location.href = "${ctx}/v2_instorageCount/toCountTaskListPage";
			    }else{
			      alert(data.content);
			      $("button").attr("disabled",false);
                   $("button").attr("class","btn btn-primary");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
				$("button").attr("disabled",false);
                $("button").attr("class","btn btn-primary");
			}
	   });
  }
  function clearBatch() {
  	$('#txt_batchCode').val('');
		$('#txt_productDate').val('');
		$('#txt_expireDate').val('');
  	$('.txt_batchCode').css('display','none');
  	$('.txt_expireDate').css('display','none');
  }
  function confirm(){
  	if($("#singleItem").children().length == 0 && $("#boxItem").children().length == 0){
	 	alert("点收数据不能为空");
	 	return;
	 }
  	jConfirm("请仔细核对sku数量是否正确!","点收确认",function(result){
  		if(result){
  			$("#finish_btn").attr("disabled",true);
			  $.ajax({
					type:'post',
					url:'${ctx}/v2_instorageCount/ajaxFinishCount',
					data:{
						taskCode:'${task.taskCode}',
						billCode:'${task.billCode}'
					},
					dataType:'json',
					success:function(data) {
					    if(data.result==true){
					    	window.location.href = "${ctx}/v2_instorageCount/toCountTaskListPage";
					    }else{
					      alert(data.content);
					      $("button").attr("disabled",false);
		                  $("button").attr("class","btn btn-primary");
					    }
					},
					error:function() {
						alert("网络繁忙，请稍后重试...");
						$("button").attr("disabled",false);
		                $("button").attr("class","btn btn-primary");
					}
			   });
  		}
  	});
  }
</script>
</head>

<body>
<section class="content">

	<table id="contentTable" class="table  table-bordered table-condensed">
	    <thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc">点收</th>
		</thead>
		<tr>
		    <td > 
		       <span style="margin-left: 10px;">是否报损件:</span>
		       <select id="error_flag" class="oldSelectStyle" onchange="errorChange();">
				     <option value="noterror">完好件</option>  
				     <option value="error">破损件</option>  
				     
				</select>
				
		       <span style="margin-left: 10px;">SKU:</span>
		       <input id="txt_sku_1" type="text" required onkeydown="if(event.keyCode == '13'){keydown_key();}" onchange="clearBatch();"/>
		       	<div style="display:none;" class="txt_batchCode">
		       		<span style="margin-left: 10px;">批次号:</span>
		       		<input id="txt_batchCode" type="text" disabled/>
		       	</div>
		       	<div style="display:none;" class="txt_expireDate">
		       		<span style="margin-left: 10px;">生产日期:</span>
		       		<input id="txt_productDate" type="text" disabled/>
		       	</div>
		       	<div style="display:none;" class="txt_expireDate">
		       		<span style="margin-left: 10px;">过期日期:</span>
		       		<input id="txt_expireDate" type="text" disabled/>
		       	</div>
		       	<span style="margin-left: 10px;">数量:</span>
		       	<input id="txt_num" type="text" oninput="numChange()" onkeydown="if(event.keyCode == '13'){numAdd();}"/>
		       	<input id="lock_num" name="lock_num" type="checkbox" onclick="alert('123')" /> <!--onLockNumChange() -->
		       	<span style="margin-left:12px; margin-top: 10px; color: red;font-weight: bold;" id="msg_1"></span>
		       
                 <%-- <button class="btn btn-primary" onclick="finish('${task.taskCode}');">完成点收</button> --%>      
                 
		    </td>
		</tr>
	</table>
	
	<!-- 数据 -->
	<table class="table" style="margin-top: 0px;">
		<thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc">已点收数据</th>
		</thead>
		<thead><tr>
			<th style="width: 50px;">序号</th>
		    <th style="width: 150px;">SKU</th>
		    <th style="width: 150px;">商品名称</th>
		    <th style="width: 250px;">数量</th>
		    <th style="width: 250px;">预约数量</th>
		    <th style="width: 250px;">批次号</th>
		    <th style="width: 250px;">生产日期</th>
		    <th style="width: 250px;">过期日期</th>
		    <th style="width: 150px;">报损状态</th>
		    <th>操作</th>
		</tr></thead>
		<tbody id="boxItem" class="text-center">
		<c:forEach items="${items}" var="t" varStatus="i">
			<c:if test="${t.num eq t.planNum }">
				<tr id="tr_${t.id}" style="backgroud:green;">
			</c:if>
			<c:if test="${t.num ne t.planNum }">
				<tr id="tr_${t.id}">
			</c:if>
			   <td>${i.count }</td>
			   <td>${t.sku}</td>
			   <td>${t.ediGoodsSku.skuName}</td>
			   <td>${t.num}</td>
			   <td>${t.planNum}</td>
			   <td>${t.batchCode}</td>
			   <td>${t.productDate}</td>
			   <td>${t.expireDate}</td>
			   <c:if test="${t.errorFlag eq 'noterror'}">
			   		<td>完好件</td>
			   </c:if>
			   <c:if test="${t.errorFlag eq 'error'}">
			   		<td>破损件</td>
			   </c:if>
			   <td><a class="btn btn-warning" onclick="deleteItem('${t.id}');">删除</a></td>
			</tr>
		</c:forEach>
		</tbody>
		
	</table>
	<a class="btn btn-primary" onclick="confirm()">全部点收完成</a>
</section>
</body>
</html>
